import requests
import json
import numpy as np
import pandas as pd
import os


url = 'http://www.ceic.ac.cn/ajax/speedsearch?num=3&&page=1'
def send_request(url):
    headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'}
    response = requests.get(url=url,headers=headers).content.decode()
    return json.loads(response[1:-1])
def analysis_data(data):
    for i in data['shuju']:
        data_dataframe = pd.DataFrame(i, index=[i['LOCATION_C']])
        yield data_dataframe


def save_svc(datas):
    for data in datas:
        if '地震数据3.csv' not in os.listdir(os.getcwd()):
            data.to_csv('地震数据3.csv', mode='a')
        else:
            data.to_csv('地震数据3.csv', mode='a', header=False)

def  run():

    datas = send_request(url)
        # 清洗数据
    data_dataframe = analysis_data(datas)
        # 保存到文件
    save_svc(data_dataframe)

run()